package cover.main;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultEdge;

import cover.alg.HGA;
import cover.vertex.Vertex;
import dot.Export;
import dot.Import;
/**
 * Main class for checking how algorithm works
 * 
 */
public class Main {

	public static void main(String[] args) throws Exception {
		boolean check = true;
		while(check){
			try{
				System.out.println("Specify a input-file path:");
				BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
				String nameOfInFile = reader.readLine();
				Import importGraph = new Import();
				//String nameOfFile = "test/Vertex6Edge29Cover6Pop1G7.dot";
				Graph<Vertex, DefaultEdge> graph = importGraph.genGraphFromDot(nameOfInFile);
				while(check){
					try{
						System.out.println("Determine number of populations. For most of graphs 3 is enough.");
						int numberOfPopulations = Integer.parseInt(reader.readLine());
						System.out.println("Specify a output-file path:");
						String nameOfOutFile = reader.readLine();
						Date start = new Date();		
						HGA vertexCover = new HGA(graph,numberOfPopulations);
						Date end = new Date();
			
						System.out.println("Algorithm time: " + (end.getTime()-start.getTime()) + "ms");
						System.out.println("Count of vertex in graph: " + graph.vertexSet().size());
						System.out.println("Count of vertex in cover: " + vertexCover.getCover().size());
						System.out.println("Count of usefull populations: " + vertexCover.getUsefull() +
								" of " + numberOfPopulations);
							
						Export export = new Export();
						export.exportToDot(nameOfOutFile, graph);
						check = false;
					}catch(NumberFormatException e){
						System.out.println("It must be integer!");
						System.out.println("Please try again.");
						check = true;
						}
					}
				}catch(FileNotFoundException e){
					System.out.println("There is not such file. Please try again.");
					check = true;
				}catch(IOException e){
					System.out.println("Please try again.");
					check = true;
				}
			}
	}

}
